<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<style>
    .box {
        width: 600px;
        margin: 50px auto;
    }
    #loginName{
        width: 80px;
        height: 30px;
        background-color: #cccccc;
        color: #122b40;
        padding: 5px;
        border-radius: 5px;
        text-align: center;
        font-weight: bolder;
        position: absolute;
        top: 0;
        right: 0;

    }
    #is{
        padding: 0;
        margin-left: 0px;
        margin-bottom: 10px;
    }
    table {
        border: 1px solid #ccc;
        margin-bottom: 10px;
    }

    th, td {
        border: 1px solid #ccc;
    }
</style>
<%--<c:if test="${sessionScope.uname == null}">
        ${pageContext.response.sendRedirect("./login.jsp")}
</c:if>--%>
<body>
<div id="loginName">${sessionScope.uname}</div>
<div class="box container row">
    <h2 class="col-md-4 col-md-offset-4">学生信息</h2>
    <div id="is" class="col-md-12">
        <input class="btn btn-primary" type="button" id="InvertSelection" value="反选"/>
    </div>

    <table class="table table-hover">
        <thead>
        <tr>
            <th style="display: none">ID</th>
            <th><input type="checkbox"/></th>
            <th>学号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>联系方式</th>
            <th>家庭住址</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody></tbody>

    </table>
    <nav aria-label="Page navigation">
        <ul class="pagination">



        </ul>
    </nav>
    <input class="btn btn-info" type="button" id="addStu" value="添加"/>
    <input class="btn btn-warning" type="button" id="deleteCheck" value="批量删除"/>
</div>
</body>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
<script>
    var currentPage = 1;
    if(window.location.search != "" && window.location.search != undefined){
        currentPage = window.location.search.substring(13);
    }
    $.ajax({
        url: "http://localhost:8080/stusys/student/getStuList?currentPage="+currentPage,
        dataType: "json",
        contentType: "application/json",
        success: function (data) {
            console.log(data);
            $.each(data[0], function (index, item) {
                var tr = $("<tr></tr>");
                var chebox = $("<td><input class='checked' type='checkbox'/></td>")
                var id = $("<td class='sid' style='display: none'>" + item.id + "</td>");
                var stu_number = $("<td>" + item.study_number + "</td>");
                var name = $("<td>" + item.name + "</td>");
                var sex = $("<td>" + (item.sex == 0?"男":"女") + "</td>");
                var phone = $("<td>" + item.phone + "</td>");
                var address = $("<td>" + item.address + "</td>");
                var operation = $("<td><a class='delete' href='javascript:0;'>删除</a> <a class='update' href='javascript:0;'>更新</a></td>")
                tr.append(id);
                tr.append(chebox);
                tr.append(stu_number);
                tr.append(name);
                tr.append(sex);
                tr.append(phone);
                tr.append(address);
                tr.append(operation);
                $("tbody").append(tr);
            })
            var page = data[1];
            var totalPages = page.totalPages;
            var currentPage = page.currentPage;
            $(".pagination").append($("<li>\n" +
                "                <a class='prev' href=\"javascript:0;\" aria-label=\"Previous\">\n" +
                "                    <span aria-hidden=\"true\">&laquo;</span>\n" +
                "                </a>\n" +
                "            </li>"));
            for (var i = 0; i < totalPages; i++) {
                var li = $("<li><a class='pageCurrent' href='javascript:0;'>"+(i+1)+"</a></li>");
                $(".pagination").append(li);
            }
            $(".pagination").append($("<li>\n" +
                "                <a class='next' href=\"javascript:0;\" aria-label=\"Next\">\n" +
                "                    <span aria-hidden=\"true\">&raquo;</span>\n" +
                "                </a>\n" +
                "            </li>"));
            var $checked = $(".checked");

            var $pageCurrent = $(".pageCurrent");
            for (var i = 0; i < $pageCurrent.length; i++) {
                $pageCurrent.click(function () {
                    location.href="http://localhost:8080/stusys/index.jsp?currentPage="+$(this).html();
                })
            }
            //上一页
            $(".prev").click(function () {
                if(currentPage - 1 == 0){
                    currentPage = 1;
                }else{
                    currentPage --;
                }
                location.href="http://localhost:8080/stusys/index.jsp?currentPage="+currentPage;
            })
            //下一页
            $(".next").click(function () {
                if(currentPage + 1 > totalPages){
                    currentPage = totalPages;
                }else{
                    currentPage++;
                }
                location.href="http://localhost:8080/stusys/index.jsp?currentPage="+currentPage;
            })

            $(".delete").click(function () {
                var tr = $(this).parents("tr").children();
                var id = $(tr[0]).html();
                $.ajax({
                    url: "http://localhost:8080/stusys/student/delStudent?id=" + id,
                    success: function (data) {
                        alert(data);
                        location.reload();
                    }
                })
            })
            $("#addStu").click(function () {
                location.href = "http://localhost:8080/stusys/stuInfo.jsp?id=0";
            })

            $(".update").click(function () {
                var tr = $(this).parents("tr").children();
                var id = $(tr[0]).html();
                location.href = "http://localhost:8080/stusys/stuInfo.jsp?id=" + id;
            })
            $("th input").click(function () {
                if($(this).prop("checked")){
                    for (var i = 0; i < $checked.length; i++) {
                        $($checked[i]).prop("checked",true);
                    }
                }else{
                    for (var i = 0; i < $checked.length; i++) {
                        $($checked[i]).prop("checked",false);
                    }
                }
            })
            $("td").on("click","input",function () {
                var count = 0;

                for (var i = 0; i < $checked.length; i++) {
                    if($($checked[i]).prop("checked")){
                        count++;
                    }
                }
                if(count == data[0].length){
                    $("th input").prop("checked",true);
                }else{
                    $("th input").prop("checked",false);
                }
            })
            $("#is").click(function () {
                for (var i = 0; i < $checked.length; i++) {
                    if($($checked[i]).prop("checked")){
                        $($checked[i]).prop("checked",false);
                    }else{
                        $($checked[i]).prop("checked",true);
                    }
                }
            })

            $("#deleteCheck").click(function () {
                var sid = new Array();
                for (var i = 0; i < $checked.length; i++) {
                    if($($checked[i]).prop("checked")){
                        console.log($($checked[i]).parent().siblings(".sid").html());
                        var id = $($checked[i]).parent().siblings(".sid").html();
                        sid.push(id);
                    }
                }
                $.ajax({
                    url:"http://localhost:8080/stusys/student/delStudentMore",
                    data:{
                        sid:sid.toString()
                    },
                    type:"post",
                    success:function (data) {
                        alert(data);
                        location.href="index.jsp";
                    }
                })
            })
        }
    })
</script>
</html>